package com.fengye.algorithms.leecode;

/**
 * @Author fengye
 * @Date 2023/4/24 11:02
 **/
public class BackspaceStringCompare844 {

    public static void main(String[] args) {
        new BackspaceStringCompare844().backspaceCompare("ab#c", "ad#c");
    }

    public boolean backspaceCompare(String s, String t) {

        StringBuilder ss = doBackSpace(s);
        StringBuilder st = doBackSpace(t);

        if(ss.toString().equals(st.toString())) {
            return true;
        }

        return false;
    }

    private StringBuilder doBackSpace(String s) {
        StringBuilder ss = new StringBuilder();
        for(int i = 0; i< s.length(); i++) {
            char c = s.charAt(i);
            if(c != '#') {
                ss.append(c);
            } else {
                if(ss.length() != 0) {
                    // 删除最后一个
                    ss.deleteCharAt(ss.length()-1);
                }
            }
        }
        return ss;
    }
}
